package com.liuguang.redis.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.liuguang.api.redis.RedisService;
import com.liuguang.common.enums.CommonResultEnum;
import com.liuguang.common.pojo.CommonResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: RedisServiceImpl
 * @Description：
 * @Author: wanghr
 * Date: 2:57 PM 2020/11/6
 * Version: 1.0
 */
@DubboService
@Service
public class RedisServiceImpl implements RedisService {

    private static final Logger logger = LoggerFactory.getLogger(RedisServiceImpl.class);

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public CommonResult set(String key, String value) {
        logger.info("传入参数：{},{}", key, value);
        try {
            ValueOperations opsForValue = redisTemplate.opsForValue();
            opsForValue.set(key, value);
            return CommonResult.success();
        } catch (Exception e) {
            logger.info("redis存储key-value失败");
            return CommonResult.fail();
        }
    }

    @Override
    public void setWithTime(String key, String value, Long min) {
        logger.info("传入参数：{},{}", key, value);
        ValueOperations opsForValue = redisTemplate.opsForValue();
        opsForValue.set(key, value, min, TimeUnit.MINUTES);
    }

    @Override
    public void setByMap(HashMap<String, String> redisMap) {
        if (CollectionUtil.isNotEmpty(redisMap)) {
            ValueOperations opsForValue = redisTemplate.opsForValue();
            redisMap.forEach((key, value) ->
                    opsForValue.set(key, value)
            );
        }
    }

    @Override
    public CommonResult setHash(String key, Map<String, Object> value, Long time) {
        HashOperations hashOperations = redisTemplate.opsForHash();
//        hashOperations.put(key, value.get());
        return null;
    }

    @Override
    public CommonResult get(String key) {
        logger.info("参数：{}", key);
        String value = (String) redisTemplate.opsForValue().get(key);
        if (StringUtils.isEmpty(value)) {
            return CommonResult.fail();
        }
        return CommonResult.success(CommonResultEnum.SUCCESS.getMsg(), value);
    }

    @Override
    public CommonResult deleteBykey(String codeKey) {
        Boolean delete = redisTemplate.delete(codeKey);
        if (delete != true) {
            return CommonResult.fail();
        }
        return CommonResult.success();
    }

    @Override
    public CommonResult expire(String key, long timeout, TimeUnit timeUnit) {
        Boolean expire = redisTemplate.expire(key, timeout, timeUnit);
        if (expire != true) {
            return CommonResult.fail();
        }
        return CommonResult.success();
    }

    @Override
    public CommonResult saveToList(String key, String value) {
        logger.info("saveToList传入参数,key:{}", key);
        logger.info("saveToList传入参数,value:{}", value);
        Long aLong = redisTemplate.opsForList().rightPush(key, value);
        return CommonResult.success("插入尾部成功，list当前个数：" + aLong);
    }

    @Override
    public CommonResult<List<String>> getList(String failMessageListKey) {
        logger.info("getList传入参数,failMessageListKey:{}", failMessageListKey);
        //这种会返回list 所以的元素，但是不会删除元素
        List<String> list = redisTemplate.opsForList().range(failMessageListKey, 0, -1);
        if (!CollectionUtils.isEmpty(list)) {
            logger.info("list个数:{}", list.size());
            return CommonResult.success("获取list成功", list);
        }
        logger.info("list个数:{}", 0);
        return CommonResult.fail();
    }

    @Override
    public CommonResult deleteListByInteger(String key, Integer integer) {
        if (integer == null) {
            return CommonResult.fail("参数Integer不能为null");
        }
        logger.info("将要pop的个数：{}", integer);
        BoundListOperations listOperations = redisTemplate.boundListOps(key);
        while (integer > 0) {
            listOperations.leftPop();
            integer--;
        }
        return CommonResult.success();
    }
}

